package com.ksyun.campus.metaserver.config;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhaozhikai
 * @date 2023/8/5 23:59
 */
@Configuration
public class ZkConfig {
    @Value("${zookeeper.addr}")
    private String address;

    @Value("${zookeeper.connectionTimeout}")
    private int DEFAULT_SESSION_TIMEOUT_MS;

    @Value("${zookeeper.sessionTimeout}")
    private int DEFAULT_CONNECTION_TIMEOUT_MS;


    @Bean
    public CuratorFramework curatorFramework() throws InterruptedException {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(
                address,
                DEFAULT_SESSION_TIMEOUT_MS,
                DEFAULT_CONNECTION_TIMEOUT_MS,
                new RetryNTimes(Integer.valueOf(2),Integer.valueOf(3000))
        );
        curatorFramework.start();
        //阻塞直到连接成功
        curatorFramework.blockUntilConnected();
        return  curatorFramework;
    }

}

